import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STChange, STRequestOptions, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';
import { TicketService } from '../../services/ticket.service';

@Component({
  selector: 'app-etc-invoiced-requested',
  templateUrl: './etc-invoiced-requested.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class ETCInvoicedRequestedComponent extends BasicTableComponent {
  @ViewChild('auditModal', { static: false })
  auditModal!: any;
  columns: STColumn[] = this.initST();
  schema: SFSchema = this.initSF();

  selectedRows: any[] = [];
  constructor(public service: TicketService, private nzModalService: NzModalService, public searchDrawerService: SearchDrawerService) {
    super(searchDrawerService);
  }

  search() {
    this.st?.load(1);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value
      });
    }
    this.selectedRows = [];
    return requestOptions;
  };

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  auditAction() {
    if (this.selectedRows?.length <= 0) {
      this.service.msgSrv.warning('请选择申请记录');
      return;
    }
    const modal = this.nzModalService.warning({
      nzTitle: '确定对已选运单批量申请开票？',
      nzOnOk: () => {
        this.service
          .request(this.service.$api_get_apply_invoice, { wayBillIds: this.selectedRows.map(item => item.id) }, 'POST', false)
          .subscribe(res => {
            if (res) {
              this.service.msgSrv.success('申请开票成功');
              this.st.load(1);
            }
            modal.destroy();
          });

        return false;
      }
    });
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        wayBillCode: {
          type: 'string',
          title: '运单号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        billCode: {
          type: 'string',
          title: '订单号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        billType: {
          type: 'string',
          title: '订单类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'bill:type' },
            placeholder: '请选择'
          }
        },
        driverName: {
          type: 'string',
          title: '司机姓名',
          ui: {
            placeholder: '请输入',
          }
        },
        driverPhone: {
          type: 'string',
          title: '司机手机',
          ui: {
            placeholder: '请输入',
          }
        },
        licenseCarNo: {
          type: 'string',
          title: '车牌号',
          ui: {
            placeholder: '请输入',
          }
        },
        licenseBelonging: {
          type: 'string',
          title: '车辆所有人',
          ui: {
            placeholder: '请输入',
          }
        },
        dischargePlace: {
          type: 'string',
          title: '卸货地',
          ui: {
            autocomplete: 'off',
            placeholder: '请选择',
          }
        },
        loadingPlace: {
          type: 'string',
          title: '装货地',
          ui: {
            autocomplete: 'off',
            placeholder: '请选择',
          }
        },
        shipperId: {
          type: 'string',
          title: '托运人',
          ui: {
            widget: 'select',
            serverSearch: true,
            searchDebounceTime: 300,
            searchLoadingText: '搜索中...',
            allowClear: true,
            onSearch: (q: any) => this.service.getEnterpriceList({ enterpriseName: q }),
          }
        },
        enterpriseInfoName: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          },
          default: ''
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox', width: '60px' },
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '运单号', index: 'wayBillCode', width: '170px' },
      { title: '订单号', index: 'billCode', width: '170px' },
      { title: '订单类型', index: 'billTypeLabel', width: '140px' },
      { title: '装货地', index: 'loadingPlace', width: '220px' },
      { title: '卸货地', index: 'dischargePlace', width: '220px' },
      { title: '司机信息', render: 'call1No', width: '140px' },
      { title: '车辆信息', render: 'call1N2o', width: '200px' },
      { title: '车牌颜色', index: 'licenseCarNOColorLabel', width: '140px' },
      { title: '车辆是否已备案', index: 'putOnRecord', width: '150px', type: 'enum', enum: { false: '否', true: '是' } },
      { title: '托运人', index: 'shipperAppUserName', width: '140px' },
      { title: '网络货运人', index: 'enterpriseInfoName', width: '220px' },
      { title: '接单时间', index: 'orderReceivingTime', type: 'date', width: '150px' },
      { title: '装货时间', index: 'loadingTime', type: 'date', width: '150px' },
      { title: '卸货时间', index: 'unloadingTime', type: 'date', width: '150px' },
      { title: '签收时间', index: 'submissionTime', type: 'date', width: '150px' }
    ];
  }
  // 导出
  exprot() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_get_asyncExportEtcApplyList);
  }
}
